//
//  UIImage+Extension.swift
//  微博
//
//  Created by 霸气的刘吉庆 on 16/4/26.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit

extension UIImage{
    
    //缩放到指定的宽度
    func scaleToWidth(width: CGFloat) -> UIImage {
        
        if width > self.size.width {
            
            return self
        }
        
        let height = width/self.size.width * self.size.height
        
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        
        //开启图形上下文
        UIGraphicsBeginImageContext(rect.size)
        
        
        //渲染,将图片的所有的内容都画到上下文指定的rect
        self.drawInRect(rect)
        
        //取出对应的rect
        let result = UIGraphicsGetImageFromCurrentImageContext()

        //关闭图形上下文队形
        UIGraphicsEndImageContext()
        return result
        
        
        
    }
    
    
    
    
    
    
    
    
}